from sqlalchemy import Integer, ForeignKey, String, SmallInteger
from sqlalchemy.orm import Mapped, mapped_column, relationship
from .baseModel import BaseModel

class User(BaseModel):
    __tablename__ = "user"

    id: Mapped[int] = mapped_column(Integer, primary_key=True)
    username: Mapped[str] = mapped_column(
        String(50), comment="用户openid", default='', nullable=False, unique=True
    )
    password: Mapped[str] = mapped_column(
        String(20), comment="用户昵称", default='', nullable=False
    )
    gold_coins: Mapped[int] = mapped_column(
        Integer, default=10, nullable=False, comment="积分"
    )

    is_supper_user: Mapped[int] = mapped_column(
        SmallInteger, default=0, nullable=False, comment="是否是超级管理员"
    )
    
    def __repr__(self):
        return f"<User(id={self.id}, username={self.username}, password={self.password})>"

class GoldCoinsInfo(BaseModel):
    __tablename__ = "gold_coins_info"

    id: Mapped[int] = mapped_column(Integer, primary_key=True)
    text: Mapped[str] = mapped_column(
        String(200), comment="积分描述信息", default='', nullable=False
    )
    gold_coins_type: Mapped[int] = mapped_column(
        Integer, comment="积分类型 1: 观看博客； 2: 邀请新用户； 3: 打卡签到", default=1, nullable=False
    )
    gold_coins: Mapped[int] = mapped_column(
        Integer, comment="积分", default=1, nullable=False
    )

    def __repr__(self):
        return f"<GoldCoinsInfo(id={self.id}, text={self.text[:10]}, gold_coins_type={self.gold_coins_type}, gold_coins={self.gold_coins})>"

class GoldCoinsLog(BaseModel):
    __tablename__ = "gold_coins_log"

    id: Mapped[int] = mapped_column(Integer, primary_key=True)
    source_user_id: Mapped[int] = mapped_column(
        Integer, ForeignKey("user.id"), comment="来源用户id", nullable=False
    )
    target_user_id: Mapped[int] = mapped_column(
        Integer, ForeignKey("user.id"), comment="目标用户id", nullable=True
    )
    gold_coins_info_id: Mapped[int] = mapped_column(
        Integer, ForeignKey("gold_coins_info.id"), comment="积分描述信息id", nullable=False
    )
    blog_id: Mapped[int] = mapped_column(
        Integer, ForeignKey("blog.id"), comment="博客id", nullable=True  # 修复表名
    )

    text: Mapped[str] = mapped_column(
        String(200), comment="日志描述", default='', nullable=False
    )

    source_user: Mapped["User"] = relationship(
        "User", backref="source_user_gold_log", foreign_keys=[source_user_id]
    )
    target_user: Mapped["User"] = relationship(
        "User", backref="target_user_gold_log", foreign_keys=[target_user_id]
    )
    gold_coins_info: Mapped["GoldCoinsInfo"] = relationship(
        "GoldCoinsInfo", backref="gold_coins_info_log", foreign_keys=[gold_coins_info_id]
    )
    blog = relationship(
        "Blog",  # 使用完整模块路径
        backref="blog_gold_coins_log",
        foreign_keys=[blog_id]
    )